package test5.lupeng.jf.com.teamdemo.adapter;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.StaggeredGridLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

import test5.lupeng.jf.com.teamdemo.R;
import test5.lupeng.jf.com.teamdemo.bean.response.ResponseLast;
import test5.lupeng.jf.com.teamdemo.untils.GlideCircleTransform;


//动态广场的适配器；
public class NewMessageAdapter extends RecyclerView.Adapter {

    ArrayList<ResponseLast.ResultBean> results;

    public NewMessageAdapter(ArrayList<ResponseLast.ResultBean> results){
        this.results = results;
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_newmessage, parent, false);
        NewMessageViewHolder holder = new NewMessageViewHolder(view);
        return holder;
    }

    public class NewMessageViewHolder extends RecyclerView.ViewHolder{

        private final ImageView iv_headImage;
        private final TextView tv_otherName;
        private final TextView tv_otherId;
        private final TextView tv_otherMessage;

        private final TextView first_count;
        private final TextView second_count;
        private final LinearLayout liner1;
        private final RecyclerView imageVp;

        public NewMessageViewHolder(View itemView) {
            super(itemView);

            //头像
            iv_headImage = itemView.findViewById(R.id.iv_headImage);
            //用户名
            tv_otherName = itemView.findViewById(R.id.tv_otherName);
            //用户Id
            tv_otherId = itemView.findViewById(R.id.tv_otherId);
            //评论
            tv_otherMessage = itemView.findViewById(R.id.tv_otherMessage);
            //发表的图片
            imageVp = itemView.findViewById(R.id.imageVp);

            //笑脸的数量
            first_count = itemView.findViewById(R.id.first_count);
            //爱心的数量
            second_count = itemView.findViewById(R.id.second_count);
            liner1 = itemView.findViewById(R.id.liner1);
        }
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        NewMessageViewHolder holder1 = (NewMessageViewHolder) holder;
        ResponseLast.ResultBean bean = results.get(position);
        Glide.with(holder1.itemView.getContext())
                .load(bean.getAuthor().getHeadProtrait())
                .centerCrop()
                .transform(new GlideCircleTransform(holder1.itemView.getContext()))
                .into(holder1.iv_headImage);

        holder1.tv_otherName.setText(bean.getAuthor().getUserName());
        holder1.tv_otherMessage.setText(bean.getMessage());

        StaggeredGridLayoutManager layoutManager = new StaggeredGridLayoutManager(3, StaggeredGridLayoutManager.VERTICAL);
        holder1.imageVp.setLayoutManager(layoutManager);

        newMessageInterface.setImage(bean.getImageUrls(),holder1.imageVp);

        holder1.first_count.setText(bean.getCommentsSize() + "");
        holder1.second_count.setText(bean.getPraiseSize() + "");

    }

    @Override
    public int getItemCount() {
        return results.size();
    }

    NewMessageInterface newMessageInterface;

    public void setListener(NewMessageInterface newMessageInterface) {
        this.newMessageInterface = newMessageInterface;
    }

    public interface NewMessageInterface{

        void setImage(List<String> list,RecyclerView rv);

    }
}
